package com.itbaizhan.openhis.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbaizhan.openhis.constants.Constants;
import com.itbaizhan.openhis.domain.DictData;
import com.itbaizhan.openhis.domain.DictType;
import com.itbaizhan.openhis.dto.DictTypeDto;
import com.itbaizhan.openhis.mapper.DictDataMapper;
import com.itbaizhan.openhis.service.DictTypeService;
import com.itbaizhan.openhis.mapper.DictTypeMapper;
import com.itbaizhan.openhis.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
* @author MR
* @description 针对表【sys_dict_type(字典类型表)】的数据库操作Service实现
* @createDate 2025-05-19 14:18:22
*/
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType>
    implements DictTypeService{
	
	@Autowired
	private DictTypeMapper dictTypeMapper;
	
	@Autowired
	private DictDataMapper dictDataMapper;
	
	@Autowired
	private StringRedisTemplate redisTemplate;
	/**
	 * 分页查询字典类型
	 * @param dictTypeDto
	 * @return
	 */
	@Override
	public DataGridView listForPage(DictTypeDto dictTypeDto) {
		
		Page<DictType> page = new Page<>(dictTypeDto.getPageNum(), dictTypeDto.getPageSize());
		// 构造查询条件
		QueryWrapper<DictType> wrapper = new QueryWrapper<>();
		wrapper.like(StringUtils.isNotBlank(dictTypeDto.getDictName()),
				DictType.COL_DICT_NAME,  dictTypeDto.getDictName());
		wrapper.eq(StringUtils.isNotBlank(dictTypeDto.getStatus()),
				DictType.COL_STATUS,  dictTypeDto.getStatus());
		wrapper.like(StringUtils.isNotBlank(dictTypeDto.getDictType()),
				DictType.COL_DICT_TYPE,  dictTypeDto.getDictType());
		// 时间查询创建时间大于开始时间
		wrapper.ge(dictTypeDto.getBeginTime() != null,
				DictType.COL_CREATE_TIME,  dictTypeDto.getBeginTime());
		// 时间查询创建时间小于结束时间
		wrapper.le(dictTypeDto.getEndTime() != null,
				DictType.COL_CREATE_TIME,  dictTypeDto.getEndTime());
		//使用MyBatis-Plus的分页查询方法，传入分页对象和查询条件
		dictTypeMapper.selectPage(page, wrapper);
		return new DataGridView(page.getTotal(),page.getRecords());
	}
	
	/**
	 * 同步字典数据到redis缓存中
	 */
	@Override
	public void dictCacheAsync() {
		//构建字典类型查询条件
		QueryWrapper<DictType> wrapper1 = new QueryWrapper<>();
		wrapper1.eq(DictType.COL_STATUS, Constants.STATUS_TRUE);
		List<DictType> dictTypes = dictTypeMapper.selectList(wrapper1);
		for (DictType dictType : dictTypes) {
			//构建字典数据查询条件
			QueryWrapper<DictData> wrapper2 = new QueryWrapper<>();
			wrapper2.eq(DictData.COL_STATUS, Constants.STATUS_TRUE);
			wrapper2.eq(DictData.COL_DICT_TYPE, dictType.getDictType());
			wrapper2.orderByDesc(DictData.COL_DICT_SORT);
			
			//查询字典数据列表
			List<DictData> dictDatas = dictDataMapper.selectList(wrapper2);
			//添加到redis缓存中
			String jsonString = JSON.toJSONString(dictDatas);
			ValueOperations<String, String> value = redisTemplate.opsForValue();
			value.set(Constants.DICT_REDIS_PROFIX + dictType.getDictType(), jsonString);
		}
	}
}




